package com.bharatonjava.swift.dao.mappers;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.data.jdbc.core.OneToManyResultSetExtractor;

import com.bharatonjava.swift.domain.Role;
import com.bharatonjava.swift.domain.User;

public class UserRoleExtractor extends
		OneToManyResultSetExtractor<User, Role, String> {

	public UserRoleExtractor() {
		super(new UserRowMapper(), new RoleMapper());
	}

	@Override
	protected void addChild(User user, Role role) {
		user.getRoles().add(role);
	}

	@Override
	protected String mapForeignKey(ResultSet rs) throws SQLException {
		if (rs.getString("R.USERNAME") == null) {
			return null;
		} else {
			return rs.getString("R.USERNAME");
		}
	}

	@Override
	protected String mapPrimaryKey(ResultSet rs) throws SQLException {
		return rs.getString("U.USERNAME");
	}

}
